import Crypt from "../../../utils/Crypt";
import UserModel from "../../../models/User";

const userModel = new UserModel()

Page({
  data: {
    nickname: '',
    bank_name: '',
    bank_number: '',
    mobile: ''
  },
  onLoad: function (options) {
    userModel.bankFetch().then(response => {
      let { data } = response
      data = Crypt.parse(data)
      data = data.bank
      this.setData({ ...data })
    })
  },

  bindNickname(e) {
    this.setData({ nickname: e.detail.value })
  },

  bindBankName(e) {
    this.setData({ bank_name: e.detail.value })
  },

  bindBankNumber(e) {
    this.setData({ bank_number: e.detail.value })
  },

  bindMobile(e) {
    this.setData({ mobile: e.detail.value })
  },

  onSave() {
    if (!this.data.nickname) {
      return wx.showToast({title: '请输入姓名', icon: 'error'})
    }
    if (!this.data.bank_name) {
      return wx.showToast({title: '请输入银行名称', icon: 'error'})
    }
    if (!this.data.bank_number) {
      return wx.showToast({title: '请输入银行卡号', icon: 'error'})
    }
    if (!this.data.mobile) {
      return wx.showToast({title: '请输入预留手机号码', icon: 'error'})
    }

    // save
    userModel.bankEdit({ ...this.data }).then(() => {
      wx.showModal({title: '保存成功', success: () => {
        wx.switchTab({
          url: '/pages/center/index'
        })
      }})
    })
  }
});
